<template>
  <div class="cartcontrol">
    <transition name="slide-fade">
      <div class="cart-decrease" v-show="food.count>0" @click.stop.prevent="decreaseCart">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <transition name="fade">
      <div class="cart-count" v-show="food.count>0">{{food.count}}</div>
    </transition>
    <div class="cart-add icon-add_circle" @click.stop.prevent="addCart"></div>
  </div>
</template>

<style lang="stylus" rel="stylesheet/stylus">
  .cartcontrol
    font-size 0
    .cart-decrease
      display inline-block
      padding 6px
      transform translate3D(0, 0, 0)
      &.slide-fade-enter-active, &.slide-fade-leave-active
        transition: all .5s linear;
      &.slide-fade-enter, &.slide-fade-leave-to
        transform translate3D(24px, 0, 0) rotate(180deg)
        opacity 0
      .inner
        display inline-block
        line-height 24px
        font-size 24px
        color rgb(0, 160, 220)
    .cart-count
      display inline-block
      vertical-align top
      width 12px
      padding-top 6px
      line-height 24px
      text-align center
      font-size 10px
      color rgb(147, 153, 159)
      &.fade-enter-active, &.fade-leave-active
        transition: all .5s linear;
      &.fade-enter, &.fade-leave-to
        opacity 0
    .cart-add
      display inline-block
      padding 6px
      line-height 24px
      font-size 24px
      color rgb(0, 160, 220)

</style>

<script type="text/ecmascript-6">
  import Vue from 'vue';
  import bus from './bus.js';

  export default {
    props: {
      food: {
        type: Object
      }
    },
    methods: {
      addCart(event) {
        // 判断是否PC端 去除一次点击 PC端点击一下=点击2下
        if (!event._constructed) {
          return;
        }
        if (!this.food.count) {
          Vue.set(this.food, 'count', 1);
        } else {
          this.food.count++;
        }
        bus.$emit('cart.add', event.target);
      },
      decreaseCart(event) {
        if (!event._constructed) {
          return;
        }
        if (this.food.count) {
          this.food.count--;
        }
      }
    }
  };
</script>
